local skel = fk.CreateSkill {
  name = "emo__liuzhi",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__liuzhi"] = "六制",
  [":emo__liuzhi"] = "出牌阶段限一次，你可以摸六张牌，再弃置至少六张牌，其中武器牌/【杀】计入三/两张。若计入一张的黑色牌仅一张，你可使用之。",

  ["#emo__liuzhi"] = "六制，你可以摸6弃6，武器牌/【杀】视为3/2张",
  ["#emo__liuzhi-discard"] = "六制，弃置至少6张牌，武器牌/【杀】视为3/2张",
  ["#emo__liuzhi-use"] = "六制：你可以使用这张牌！",

  ["$emo__liuzhi1"] = "不急，吾等，必一击制敌。",
  ["$emo__liuzhi2"] = "纵横捭阖，自有制衡之道。",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#emo__liuzhi",
  card_num = 0,
  target_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    player:drawCards(6, skel.name)
    if player.dead then return end
    local canDiscards = table.filter(player:getCardIds("he"), function (id)
      return not player:prohibitDiscard(id)
    end)
    if #canDiscards == 0 then return end
    local getValue = function (id)
      local c = Fk:getCardById(id)
      if c.sub_type == Card.SubtypeWeapon then
        return 3
      elseif c.trueName == "slash" then
        return 2
      end
      return 1
    end
    --- 选择弃牌
    ---@return integer[]
    local chosenCards = function ()
      local all = 0
      for _, id in ipairs(canDiscards) do
        all = all + getValue(id)
      end
      if all < 6 then return canDiscards end
      local _, dat = room:askToUseActiveSkill(player, {
        skill_name = "emo__liuzhi_select", prompt = "#emo__liuzhi-discard", cancelable = false,
      })
      if dat then
        return dat.cards
      else
        table.sort(canDiscards, function (a, b)
          return getValue(a) > getValue(b)
        end)
        local selected = {}
        all = 0
        for _, id in ipairs(canDiscards) do
          all = all + getValue(id)
          table.insert(selected, id)
          if all >= 6 then
            break
          end
        end
        return selected
      end
    end
    local cards = chosenCards()
    local black = table.filter(cards, function (id)
      return Fk:getCardById(id).color == Card.Black and getValue(id) == 1
    end)
    room:throwCard(cards, skel.name, player, player)
    if not player.dead and #black == 1 then
      local cid = black[1]
      if room:getCardArea(cid) == Card.DiscardPile and not player:prohibitDiscard(Fk:getCardById(cid)) then
        room:askToUseRealCard(player, {
          pattern = {cid}, expand_pile = {cid}, skill_name = skel.name, skip = false,
          prompt = "#emo__liuzhi-use",
        })
      end
    end
  end,
})

return skel
